<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="车款名称" prop="carName">
        <el-input
          v-model="queryParams.carName"
          placeholder="请输入车款名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['car:info:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['car:info:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['car:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车款ID" align="center" prop="carId" />
      <el-table-column label="品牌ID" align="center" prop="brandId" :formatter="fmtBrand"/>
      <el-table-column label="车系ID" align="center" prop="seriesId" :formatter="fmtSeries"/>
      <el-table-column label="车型ID" align="center" prop="modelId" :formatter="fmtMode"/>
      <el-table-column label="车款名称" align="center" prop="carName" />
<!--      <el-table-column label="介绍" align="center" prop="content" />-->
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['car:info:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['car:info:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改车辆车款对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌" prop="brandId">
          <el-select v-model="form.brandId" placeholder="请选择品牌" clearable size="small">
            <el-option
              v-for="dict in dictBrand"
              :key="dict.brandId"
              :label="dict.brandName"
              :value="dict.brandId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车系" prop="seriesId">
          <el-select v-model="form.seriesId" placeholder="请选择车系" clearable size="small">
            <el-option
              v-for="dict in dictSeries"
              :key="dict.seriesId"
              :label="dict.seriesName"
              :value="dict.seriesId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车型" prop="seriesId">
          <el-select v-model="form.modelId" placeholder="请选择车型" clearable size="small">
            <el-option
              v-for="dict in dictModel"
              :key="dict.modelId"
              :label="dict.modelName"
              :value="dict.modelId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="车款名称" prop="carName">
          <el-input v-model="form.carName" placeholder="请输入车款名称" />
        </el-form-item>
        <el-form-item label="介绍">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!-- 修改车款详细信息-->
    <el-dialog :title="car.title" :visible.sync="car.open.main" width="auto" append-to-body @close="handleQuery">
      <div class="app-container">
        <el-row :gutter="20">
          <!--部门数据-->
          <el-col :span="4" :xs="24">
<!--            <div class="head-container">
              <el-input
                v-model="deptName"
                placeholder="请输入部门名称"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
              />
            </div>-->
            <div class="head-container">
              <el-tree
                :data="deptOptions"
                :props="defaultProps"
                :expand-on-click-node="false"
                :filter-node-method="filterNode"
                ref="tree"
                default-expand-all
                @node-click="handleNodeClick"
              />
            </div>
          </el-col>
          <!--用户数据-->
          <el-col :span="20" :xs="24">
            <div v-show="car.open.car_info">
              <car-info ref="car_info" :car-info-id="ids" bus-type="car_info"></car-info>
            </div>
            <div v-show="car.open.car_config_dl">
              <car_config_dl ref="car_config_dl" :car-info-id="ids" bus-type="car_config_dl"></car_config_dl>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listInfo, delInfo, addInfo, updateInfo } from "@/api/car/info";
import {listBrand} from "@/api/car/brand";
import {listSeries} from "@/api/car/series";
import {listModel} from "@/api/car/model";

import CarInfo from "@/views/car/info/car_info";
import Car_config_dl from "@/views/car/info/car_config_dl";
import {selectDictLabel} from "@/utils/ruoyi";

export default {
  name: "CarInfoList",
  components: {Car_config_dl, CarInfo },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆车款表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brandId: null,
        seriesId: null,
        modelId: null,
        carName: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        brandId: [
          { required: true, message: "品牌ID不能为空", trigger: "change" }
        ],
        seriesId: [
          { required: true, message: "车系ID不能为空", trigger: "change" }
        ],
        modelId: [
          { required: true, message: "车型ID不能为空", trigger: "change" }
        ],
      },
      dictSeries: [],
      dictBrand: [],
      dictModel: [],
      car: {
        title: '编辑',
        open: {
          default: 'car_info',
          main: false,
          car_info: true,
          car_config_dl: false,
        },
      },
      deptName: '',
      deptOptions: [{id:'car_info',label:'车辆介绍',menu: true,children:[]},
        {id:'car_config',label:'车辆参数配置',menu: false,children:[
            {id:'car_config_dl',label:'动力系统参数',menu: true,children:[]},
            {id:'car_config_gn',label:'功能配置',menu: true,children:[]},
            {id:'car_config_aq',label:'安全参数',menu: true,children:[]},
            {id:'car_config_xj',label:'悬架系统',menu: true,children:[]},
            {id:'car_config_cc',label:'车辆尺寸参数',menu: true,children:[]},
            {id:'car_config_cc',label:'车辆重量参数',menu: true,children:[]},
          ]},
        {id:'car_cl',label:'整车测量数据',menu: false,children:[
            {id:'car_cl_ww',label:'外围测量',menu: true,children:[]},
            {id:'car_cl_nb',label:'内部测量',menu: true,children:[]},
            {id:'car_cl_wb',label:'车身间隙测量',menu: true,children:[]},
            {id:'car_cl_dp',label:'地盘测量',menu: true,children:[]},
          ]}],
      defaultProps: {
        children: "children",
        label: "label"
    },
    };
  },
  created() {
    listBrand({}).then(response => {
      this.dictBrand = response.rows;
      this.loading = false;
      for (const i in this.dictBrand) {
        let row=this.dictBrand[i];
        row.value=row.brandId
        row.label=row.brandName
      }
    });
    listSeries({}).then(response => {
      this.dictSeries = response.rows;
      this.loading = false;
      for (const i in this.dictSeries) {
        let row=this.dictSeries[i];
        row.value=row.seriesId
        row.label=row.seriesName
      }
    });
    listModel({}).then(response => {
      this.dictModel = response.rows;
      this.loading = false;
      for (const i in this.dictModel) {
        let row=this.dictModel[i];
        row.value=row.modelId
        row.label=row.modelName
      }
    });
    this.getList();
  },
  methods: {
    fmtSeries(row, column, cellValue, index){
      const t=selectDictLabel(this.dictSeries,cellValue)||cellValue
      return t;
    },
    fmtBrand(row, column, cellValue, index){
      const t=selectDictLabel(this.dictBrand,cellValue)||cellValue
      return t;
    },
    fmtMode(row, column, cellValue, index){
      const t=selectDictLabel(this.dictModel,cellValue)||cellValue
      return t;
    },
    /** 查询车辆车款列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        carId: null,
        brandId: null,
        seriesId: null,
        modelId: null,
        carName: null,
        content: null,
        status: "0",
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.carId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆车款";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const carId = row.carId || this.ids
      this.ids=carId
      this.car.open.main=true
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.carId != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const carIds = row.carId || this.ids;
      this.$modal.confirm('是否确认删除车辆车款编号为"' + carIds + '"的数据项？').then(function() {
        return delInfo(carIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('car/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      if (!data.menu){
        return;
      }
      // console.log(this.car.open);
      console.log(data.id,'-',this.car.open.default)
      if (data.id!=this.car.open.default){
        this.car.open[this.car.open.default]=false
      }else{
        return;
      }
      console.log(this.car.open);
      this.car.open[data.id]=true
      this.car.open.default=data.id
      console.log(data.id)
      console.log(this.$refs);
      this.$refs[data.id].getInitData();
    },
  }
};
</script>
